pragma Singleton

import QtQuick 2.15
import QtQuick.Controls 2.15
import FluentUI 1.0
import Qt.labs.platform 1.1
import FileHelper 1.0

FluObject {
    id: fluobject

    property var navigationView
    property var paneItemMenu
    property var mainWindow

    function rename(item, newName) {
        if (newName && newName.trim().length > 0) {
            item.title = newName
        }
    }

    function getRecentlyAddedData() {
        var arr = []
        var items = navigationView.getItems()
        for (var i = 0; i < items.length; i++) {
            var item = items[i]
            if (item instanceof FluPaneItem && item.extra && item.extra.recentlyAdded) {
                arr.push(item)
            }
        }
        arr.sort(function (o1, o2) {
            return o2.extra.order - o1.extra.order
        })
        return arr
    }

    function getRecentlyUpdatedData() {
        var arr = []
        var items = navigationView.getItems()
        for (var i = 0; i < items.length; i++) {
            var item = items[i]
            if (item instanceof FluPaneItem && item.extra && item.extra.recentlyUpdated) {
                arr.push(item)
            }
        }
        return arr
    }

    function getSearchData() {
        if (!navigationView) {
            return
        }
        var arr = []
        var items = navigationView.getItems()
        for (var i = 0; i < items.length; i++) {
            var item = items[i]
            if (item instanceof FluPaneItem) {
                if (item.parent instanceof FluPaneItemExpander) {
                    arr.push({
                                 "title": `${item.parent.title} -> ${item.title}`,
                                 "key": item.key
                             })
                } else
                    arr.push({
                                 "title": item.title,
                                 "key": item.key
                             })
            }
        }
        return arr
    }

    function startPageByItem(data) {
        navigationView.startPageByItem(data)
    }

    Component {
        id: menuFolder
        FluPaneItemExpander {}
    }

    Component {
        id: menuSeparator
        FluPaneItemSeparator {
            spacing: 10
            size: 1
        }
    }

    Component {
        id: menuNavItem
        FluPaneItem {
            menuDelegate: paneItemMenu
            url: ""
            onTap: {
                navigationView.push(url)
            }
        }
    }

    // Component {
    //     id: menuDiagItem
    //     FluPaneItem {
    //         menuDelegate: paneItemMenu
    //         url: ""
    //         onTap: {
    //             // 通过 qrc url 加载并打开弹窗
    //             var popupComponent = Qt.createComponent(url)
    //             if (popupComponent.status === Component.Ready) {
    //                 var popup = popupComponent.createObject(navigationView)
    //                 popup.open()
    //             } else {
    //                 console.error("Failed to load popup component:", popupComponent.errorString())
    //             }
    //         }
    //     }
    // }
    Component {
        id: menuDiagItem
        FluPaneItem {
            menuDelegate: paneItemMenu
            url: ""
            onTap: {

                // 打开模态窗体
                FluRouter.navigate(url)
            }
        }
    }
    Component {
        id: menuWinItem
        FluPaneItem {
            menuDelegate: paneItemMenu
            url: ""
            onTapListener: function () {
                FluRouter.navigate(url)
            }
        }
    }

    // FluLoader {
    //     id: loader
    // }
    function createMenuItem(parent, item) {
        // key      菜单键(最好唯一)
        // title    菜单标签
        // type     菜单项樊型 group item sep
        // url      动作路径
        // disabled 是否禁用(true禁用该菜单项)
        // icon     图标
        // sort     排序
        // extra    附加信息
        // children 子菜单
        let obj
        switch (item.type) {
        case "group":
            obj = menuFolder.createObject(parent, {
                                              "title": qsTr(item.title),
                                              "isExpand": item.isExpand,
                                              "icon": FluentIcons[item.icon]
                                          })
            parent.children.push(obj)
            for (const child of item.children) {
                createMenuItem(obj, child)
            }
            break
        case "sep":
            obj = menuSeparator.createObject(parent)
            parent.children.push(obj)
            break
        default:
            if (item.wintype === "win") {
                obj = menuWinItem.createObject(parent, {
                                                   "title": qsTr(item.title),
                                                   "icon": FluentIcons[item.icon],
                                                   "extra": item.extra,
                                                   "url": item.url
                                               })
            } else if (item.wintype === "diag") {
                obj = menuDiagItem.createObject(parent, {
                                                    "title": qsTr(item.title),
                                                    "icon": FluentIcons[item.icon],
                                                    "extra": item.extra,
                                                    "url": item.url
                                                })
            } else {
                obj = menuNavItem.createObject(parent, {
                                                   "title": qsTr(item.title),
                                                   "icon": FluentIcons[item.icon],
                                                   "extra": item.extra,
                                                   "url": item.url
                                               })
            }
            parent.children.push(obj)
            break
        }
    }

    Component.onCompleted: {
        const jsonStr = FileHelper.readText("tree_data.json")
        const parsedData = JSON.parse(jsonStr)
        if (Array.isArray(parsedData)) {
            for (const item of parsedData) {
                createMenuItem(fluobject, item)
            }
        }
    }
}
